<?php
namespace Home\Controller;
use Think\Controller;
/**
 * 获取修改公告控制器
 * @author 隆宗益
 *
 */
class WorkplaceController extends CommonController{

    public function index(){}

    /**
     * 获取公告
     */
	public function broadcast(){
    	/**
    	 * 需要验证权限
    	 */
		$uid = session('uid');

		$dept_id = M('user_dept_role')->where("user_id = $uid")->field('dept_id')->select();

        $personalInfo = M('user');
        $name = $personalInfo->where("user_id = $uid")->getField('user_name');

    	if($dept_id != null)
    	{

	    	$field_array = array(
                'notice_id' => 'id',
                'notice_title' => 'title',
                'notice_content' => 'content',
                'notice_create_time' => 'time',
                'notice_person_id' => 'person',
	    	);

	    		$Notice = M('notice');
		    	foreach ($dept_id as  $value) {
		    		$dept_id = $value['dept_id'];
                    $dept_name = M('department')->where("dept_id = $dept_id")->getField('dept_name');
			    	$notice[$dept_name] = $Notice->where("notice_dept_id = $dept_id")
			    					 ->order('notice_id desc')
			    					 ->field($field_array)
			    					 ->select();

                    $j = count($notice[$dept_name]);

                    for ($i=0; $i < $j; $i++) {
                        $notice[$dept_name][$i]['time'] = date('Y-m-d H:i:s',$notice[$dept_name][$i]['time']);
                        $notice[$dept_name][$i]['type']='broadcast';
                        $person_id = $notice[$dept_name][$i]['person'];
                        $name = $personalInfo->where("user_id = $person_id")->getField('user_name');
                        $notice[$dept_name][$i]['person'] = $name;
                    }
                    if ($notice[$dept_name]==null) {
                        $notice[$dept_name] = array();
                        }
			    }


	    	$this->ajaxReturn($notice);

    	}
    	else
    	{
    		$this->send_http_status(403);
            $notice = array(
    					'error'=>'权限不够',
    					'state'=>'403',
    		);
    		$this->ajaxReturn($notice);

    	}
    }

    /**
     * 修改公告
     */
    public function broadcastEdit(){

        $data1 = strip_tags($GLOBALS['HTTP_RAW_POST_DATA']) ;
        $data = json_decode($data1, true);

        $notice_id = $data['id'];

        if ($data['content'] != null && $data['title'] != null) {
            $new_data['notice_content'] = $data['content'];
            $new_data['notice_title'] = $data['title'];
            $Notice = M('notice');

            if($Notice->where("notice_id = $notice_id ")->save($new_data))
            {
                $result['id'] = $data['id'];
                $result['type'] = 'broadcast';
                $result['state'] = 'OK';

                $this->ajaxReturn($result);
            }
            else
            {
                $result['id'] = $data['id'];
                $result['type'] = 'broadcast';
                $result['error'] = '错误！';
                $result['state'] = '403';
                send_http_status(403);
                $this->ajaxReturn($result);
            }

        }

		else
        {
            $result['id'] = $data['id'];
            $result['type'] = 'broadcast';
            $result['error'] = '标题或内容不能为空';
            $result['state'] = '403';
            send_http_status(403);
            $this->ajaxReturn($result);
        }
    }

    //发布公告
    public function broadcastPub(){

        $data1 = strip_tags($GLOBALS['HTTP_RAW_POST_DATA']) ;
        $data = json_decode($data1,true);

        $uid = session('uid');
        $dept = $data['department'];
      
        $dept = "'".$dept."'";
       
        $dept_id = M('department')->where("dept_name = $dept")->getField('dept_id');
       
            if ($data['content'] != null&&$data['title'] != null ) {
                $new_data['notice_content'] = $data['content'];
                $new_data['notice_title'] = $data['title'];
                $new_data['notice_dept_id'] = $dept_id;
                $new_data['notice_create_time'] = time();
                $new_data['notice_person_id'] = $uid;
                //print_r($new_data);
                $Notice = M('notice');
                    
                    if($Notice->data($new_data)->add())
                    {
                        $result['id'] = $data['id'];
                        $result['type'] = 'broadcast';
                        $result['state'] = 'OK';
                        
                        $this->ajaxReturn($result);
                    }
                    else
                    {
                        $result['id'] = $data['id'];
                        $result['type'] = 'broadcast';
                        $result['error'] = '错误！';
                        $result['state'] = '403';
                        send_http_status(403);
                        $this->ajaxReturn($result);
                    }
            }
            else
            {
                $result['id'] = $data['id'];
                $result['type'] = 'broadcast';
                $result['error'] = '公告不能为空！';
                $result['state'] = '403';
                send_http_status(403);
                $this->ajaxReturn($result);
            }

    }
    
    /**
     * 获取个人信息
     */
    
    public function personal()
    {
    	$uid = session('uid');
    	
    	if($uid != null)
    	{	
    		$field_array = array(
    				'user_id' => 'id',
    				'user_stu_num' => 'std_id',
    				'user_name' => 'std_name',
    				'user_tel' => 'std_phone',
    				'user_qq' => 'std_qq',
    				'user_email' => 'std_email',
    		);
    		$personal = M('user');
  
    		$personalInfo = $personal->where("user_id = $uid")   		
							    		 ->field($field_array)
							    		 ->find();
    	
    		//获取导师
    		$teachers = M('user_tutor')->where("user_id = $uid")
							    	   ->join('inner join user on user_tutor.tutor_id = user.user_id')
							    	   ->field("user_name")
							    	   ->select();
    		foreach ($teachers as $v0)
    		{
    			$new_department[] = $v0['dept_name'];
    		}
    		$personalInfo['std_teacher'] = implode(',', $teachers);
    		
    		//获取部门
    		$department = M('user_dept_role')
    		->join('inner join department on user_dept_role.dept_id = department.dept_id')
    		->where("user_id = $uid")
    		->field('dept_name')
    		->select();
    		
    		foreach ($department as $v)
    		{
    			$new_department[] = $v['dept_name'];
    		}
    		$personalInfo['std_department'] = implode(',', $new_department);

    		
    		$personalInfo['type']='personal';
 
    		$this->ajaxReturn($personalInfo);
    		 
    	}
    	else
    	{
            send_http_status(403);
    		$personalInfo = array(
    				'error'=>'错误',
    				'state'=>'403',
                    
    		);
    		$this->ajaxReturn($personalInfo);
    	
    	}
    }
    
    //修改个人信息
    public function personalEdit()
    {
    	 $data1 = strip_tags($GLOBALS['HTTP_RAW_POST_DATA']) ;

        $data = json_decode($data1,true);
        //$data = I('post.');
    	$uid = session('uid');
    	$new_data['user_tel'] = $data['std_phone'];
    	$new_data['user_qq'] = $data['std_qq'];
    	$new_data['user_email'] = $data['std_email'];
    	if($new_data['user_tel']!=null&&$new_data['user_qq']!=null&&$new_data['user_email']!=null)
    	{
            M('user')->where("user_id = $uid")->save($new_data);
    		$result['id'] = $uid;
    		$result['type'] = 'personal';
    		$result['state'] = '200';
    		
    		$this->ajaxReturn($result);
    	}
    	else 
    	{
    		$result['id'] = $uid;
    		$result['type'] = 'personal';
    		$result['state'] = '403';
            $result['error'] = '个人信息不能为空';
    		
    		$this->ajaxReturn($result);
    	}

    }
    //任务数罗列
    public function mission()
    {
    	$uid = session('uid');
        
    	$time = date('Y-m-d H:i:s',time());
        $time = "'".$time."'";
    	$user_work = M('user_work');

    	$SuccessMission = $user_work
    					 ->where("user_id = $uid")
    					 ->join('inner join work on user_work.uw_work_id = work.work_id')
    					 ->join('inner join workup on user_work.uw_work_id = workup.up_work_id')
    					 ->where("up_create_time <= work_deadline_time")
    					 ->count('distinct up_work_id');
        //user_work中的未到期的任务(选择人的任务)
		$liveMission1 = $user_work
    					 ->where("user_id = $uid")
    					 ->join('inner join work on user_work.uw_work_id = work.work_id')
    					 ->where("$time < work_deadline_time")
    					 ->count();
        //work中的未到期的任务(角色任务,集体)
        $liveMission2 = M('user_dept_role')
                         ->join('inner join work on user_dept_role.role_id = work.work_role_id')
                         ->where("$time < work_deadline_time and user_id = $uid")
                         ->count();
        //全网校未到期任务     
        $liveMission3 = M('work')
                        ->where("work_dept_id = 6 and $time < work_deadline_time")
                        ->count();

		//未到期且完成的任务
		$liveover = $user_work
    					 ->where("user_id = $uid")
    					 ->join('inner join work on user_work.uw_work_id = work.work_id')
    					 ->join('inner join workup on user_work.uw_work_id = workup.up_work_id')
    					 ->where("$time < work_deadline_time and up_create_time < work_deadline_time")
    					 ->count('distinct up_work_id');
                         
        // echo $liveMission1.'<br>';
        // echo $liveMission2.'<br>';
        // echo $liveMission3.'<br>';
        // echo $liveover.'<br>';
        // echo $SuccessMission.'<br>';
		if ($liveMission1 == null) {
            $liveMission1 = 0;
        }
        if ($liveMission2 == null) {
            $liveMission2 = 0;
        }
        if ($liveMission3 == null) {
            $liveMission3 = 0;
        }
        if ($liveover == null) {
            $liveover = 0;
        }
        if ($SuccessMission == null) {
            $SuccessMission = 0;
        }
		$RunningMission = $liveMission1 + $liveMission3 + $liveMission2 - $liveover;

		$mission['id'] = $uid;
		$mission['type'] = 'missionCount';
		$mission['SuccessMission'] = $SuccessMission;
		$mission['RunningMission'] = $RunningMission;
		
 		$this->ajaxReturn($mission);
			
    }
    
 
}